<template>
  <div :class="{'mobile-legal-components': showMobilePage}" class="checkbox-inputs-wrap">
    <el-checkbox-group v-if="!isExhibition && selectData[checkboxGroupField]" v-model="selectData[checkboxGroupField]" @change="handler">
      <el-checkbox v-for="(item, index) in inputItems" :label="item.value" :key="index" :disabled="checkboxDisable(item.value)">
        <div class="input-group">
          <span class="label-name">{{ item.label }}</span>
          <el-form-item v-if="displayInput(item)" :prop="moduleName + '.' + item.field" label-width="80px" label="">
            <div class="form-item">
              <el-input v-model="selectData[item.field]" :placeholder="item.placeholder" :maxlength="256" type="textarea" row="5" resize="none"/>
              <span v-if="item.tip" class="tip">{{ item.tip }}</span>
            </div>
          </el-form-item>
        </div>
      </el-checkbox>
    </el-checkbox-group>
    <div v-if="isExhibition">
      <div v-for="(item, index) in inputItems" :key="index">
        <div v-if="selectData && selectData[checkboxGroupField].indexOf(item.value) > -1">
          <span>{{ selectData[item.field] || '--' }}</span>
          <span style="color: #666;">({{ item.label }})</span>
        </div>
      </div>
      <div v-if="isShowDetail">
        --
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex';
export default {
  name: 'CheckboxInputs',
  model: {
    prop: 'params',
    event: 'change'
  },
  props: {
    params: {
      type: Object,
      default: () => {
        return null;
      }
    },
    moduleName: {
      type: String,
      default: ''
    },
    checkboxGroupField: { // 每个输入框对应绑定的字段
      type: String,
      default: ''
    },
    inputItems: { // 输入框的类型的一个数组
      type: Array,
      default: () => []
    },
    isExhibition: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    }),
    selectData: {
      get() {
        return this.params;
      },
      set(val) {
        this.$emit('change', val);
      }
    },
    isShowDetail() {
      let count = 0;
      if (!this.selectData) return true;
      this.inputItems.forEach(item => {
        if (this.selectData[this.checkboxGroupField].indexOf(item.value) > -1) {
          count += 1;
        }
      });
      return count === 0;
    }
  },
  methods: {
    checkboxDisable(value) {
      const checkboxGroup = this.selectData[this.checkboxGroupField] || [];
      return checkboxGroup.length === 1 && checkboxGroup[0] === value;
    },
    displayInput(item) {
      const checkboxGroup = this.selectData[this.checkboxGroupField] || [];
      return checkboxGroup.indexOf(item.value) >= 0;
    },
    handler(value) {
      this.inputItems.forEach(item => {
        if (value.indexOf(item.value) < 0) {
          this.selectData[item.field] = '';
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.checkbox-inputs-wrap {
  & ::v-deep {
    .el-checkbox {
      display: block;
      margin-bottom: 15px;
    }
    .el-checkbox:last-child {
      margin-bottom: 0px;
    }
    .el-textarea__inner {
      max-width: unset !important;
      min-width: unset !important;
      width: 300px;
      height: 80px;
      font-size: 14px;
    }
  }
}
.form-item {
  display: flex;
  flex-direction: column;
  .tip {
    color: #606266;
    font-size: 12px;
  }
}
.input-group {
  display: flex;
  .label-name {
    display: inline-block;
    margin-right: 10px;
    color: #606266;
  }
}
.mobile-legal-components {
  .form-item {
    .tip {
      white-space: normal;
    }
  }
  ::v-deep .el-checkbox{
    margin-right: 0 !important;
  }
}
</style>
